﻿using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;

namespace HYCodeBase.ObjectPersistentSuit
{
	public class BasePersistentManger
	{
		public BasePersistentManger(AssemblyFileFilterDelegate inputDelegate = null)
		{
			this.m_useDelegate = inputDelegate;
			AssemblyUtility.m_useFilterDelegate = inputDelegate;
		}

		public string BasePath
		{
			get
			{
				return this.m_strBasePath;
			}
			set
			{
				this.m_strBasePath = value;
			}
		}

		public string BaseSuffixation
		{
			get
			{
				return this.m_strBaseSuffixation;
			}
			set
			{
				this.m_strBaseSuffixation = value;
			}
		}

		public bool TryCopyAPersistentClass(object inputObject, out Exception catchException, out object copyValue)
		{
			catchException = null;
			copyValue = null;
			this.m_useBinFormatter = new BinaryFormatter();
			bool result;
			try
			{
				using (new MemoryStream())
				{
					this.m_useBinFormatter.Serialize(this.m_useWriteStream, inputObject);
					this.m_useWriteStream.Seek(0L, SeekOrigin.Begin);
					copyValue = this.m_useBinFormatter.Deserialize(this.m_useWriteStream);
					result = true;
				}
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		public bool TryWriteAPersistentClassByString(object inputObject, out Exception catchException, out string strClass, bool ifUseBinder = true)
		{
			strClass = null;
			catchException = null;
			this.m_useBinFormatter = new BinaryFormatter();
			if (ifUseBinder)
			{
				this.m_useBinFormatter.Binder = new UseBinder(inputObject.GetType());
			}
			bool result;
			try
			{
				using (this.m_useWriteStream = new MemoryStream())
				{
					this.m_useBinFormatter.Serialize(this.m_useWriteStream, inputObject);
					this.m_useWriteStream.Position = 0L;
					byte[] array = new byte[this.m_useWriteStream.Length];
					this.m_useWriteStream.Read(array, 0, array.Length);
					this.m_useWriteStream.Flush();
					strClass = Convert.ToBase64String(array);
				}
				this.m_useWriteStream = null;
				result = true;
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		public bool TryGetAPersistentClassByString<X>(string strClass, out X findValue, out Exception catchException, bool ifUseBinder = true) where X : class
		{
			catchException = null;
			findValue = default(X);
			this.m_useBinFormatter = new BinaryFormatter();
			if (ifUseBinder)
			{
				this.m_useBinFormatter.Binder = new UseBinder(typeof(X));
			}
			bool result;
			try
			{
				byte[] buffer = Convert.FromBase64String(strClass);
				using (this.m_ReadStream = new MemoryStream(buffer))
				{
					findValue = (this.m_useBinFormatter.Deserialize(this.m_ReadStream) as X);
					this.m_ReadStream.Flush();
				}
				this.m_ReadStream = null;
				result = (findValue != null);
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		public bool TryWriteAPersistentClassByFile(string inputFileName, object inputObject, out Exception catchException, bool ifInputIsFullPath = false)
		{
			catchException = null;
			this.m_useBinFormatter = new BinaryFormatter();
			this.m_useBinFormatter.Binder = new UseBinder(inputObject.GetType());
			bool result;
			try
			{
				string path;
				if (!ifInputIsFullPath)
				{
					path = this.GetFullFileName(inputFileName, false);
				}
				else
				{
					path = inputFileName;
				}
				using (this.m_useWriteStream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
				{
					this.m_useBinFormatter.Serialize(this.m_useWriteStream, inputObject);
				}
				this.m_useWriteStream = null;
				result = true;
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		public bool TryGetAPersistentClassByFile<X>(string inputFileName, out X findValue, out Exception catchException, bool ifInputIsFullPath = false) where X : class
		{
			catchException = null;
			findValue = default(X);
			this.m_useBinFormatter = new BinaryFormatter();
			this.m_useBinFormatter.Binder = new UseBinder(typeof(X));
			bool result;
			try
			{
				string path;
				if (!ifInputIsFullPath)
				{
					path = this.GetFullFileName(inputFileName, false);
				}
				else
				{
					path = inputFileName;
				}
				using (this.m_ReadStream = File.OpenRead(path))
				{
					findValue = (this.m_useBinFormatter.Deserialize(this.m_ReadStream) as X);
				}
				this.m_ReadStream = null;
				result = (findValue != null);
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		public bool TryWriteAPersistentClassByXML(string inputFileName, object inputObject, out Exception catchException, bool ifInputIsFullPath = false)
		{
			catchException = null;
			bool result;
			try
			{
				string path;
				if (!ifInputIsFullPath)
				{
					path = this.GetFullFileName(inputFileName, true);
				}
				else
				{
					path = inputFileName;
				}
				using (StreamWriter streamWriter = new StreamWriter(path))
				{
					new XmlSerializer(inputObject.GetType()).Serialize(streamWriter, inputObject);
				}
				result = true;
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		public bool TryGetAPersistentClassByXML<X>(string inputFileName, out X findValue, out Exception catchException, bool ifInputIsFullPath = false) where X : class
		{
			findValue = default(X);
			catchException = null;
			bool result;
			try
			{
				string path;
				if (!ifInputIsFullPath)
				{
					path = this.GetFullFileName(inputFileName, true);
				}
				else
				{
					path = inputFileName;
				}
				using (StreamReader streamReader = new StreamReader(path))
				{
					XmlSerializer xmlSerializer = new XmlSerializer(typeof(X));
					findValue = (xmlSerializer.Deserialize(streamReader) as X);
				}
				result = true;
			}
			catch (Exception ex)
			{
				catchException = ex;
				result = false;
			}
			return result;
		}

		private string GetFullFileName(string inputFileName, bool ifIsXml = false)
		{
			if (!ifIsXml)
			{
				return this.BasePath + inputFileName + this.BaseSuffixation;
			}
			return this.BasePath + inputFileName + this.m_strBaseSuffixationOfXml;
		}

		private string m_strBasePath = "C:\\";

		private string m_strBaseSuffixation = ".Data";

		private string m_strBaseSuffixationOfXml = ".xml";

		private Stream m_useWriteStream;

		private Stream m_ReadStream;

		private BinaryFormatter m_useBinFormatter = new BinaryFormatter();

		private AssemblyFileFilterDelegate m_useDelegate;
	}
}
